{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Async `FunctionTool`\n",
    "Notebook showing how to create a `FunctionTool` that supports both synchronous and asynchronous execution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.tools.function_tool import FunctionTool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_numbers(x: int, y: int) -> int:\n",
    "    \"\"\"\n",
    "    Adds the two numbers together and returns the result.\n",
    "    \"\"\"\n",
    "    print(\"add_numbers called\")\n",
    "    return x + y\n",
    "\n",
    "\n",
    "async def async_add_numbers(x: int, y: int) -> int:\n",
    "    \"\"\"\n",
    "    Adds the two numbers together and returns the result.\n",
    "    \"\"\"\n",
    "    print(\"async_add_numbers called\")\n",
    "    return x + y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "add_numbers called\n",
      "add_numbers called\n",
      "async_add_numbers called\n"
     ]
    }
   ],
   "source": [
    "addition_func_tool = FunctionTool.from_defaults(\n",
    "    fn=add_numbers, async_fn=async_add_numbers\n",
    ")\n",
    "\n",
    "# Two ways to call the function synchronously\n",
    "assert addition_func_tool(1, 2).raw_output == 3\n",
    "assert addition_func_tool.call(1, 2).raw_output == 3\n",
    "\n",
    "# One way to call the function asynchronously\n",
    "assert (await addition_func_tool.acall(1, 2)).raw_output == 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Use the tool in an `OpenAIAgent`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dotenv\n",
    "\n",
    "dotenv.load_dotenv(\"../../.env\")\n",
    "from llama_index.agent import OpenAIAgent\n",
    "\n",
    "agent = OpenAIAgent.from_tools([addition_func_tool])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "add_numbers called\n",
      "50 + 17 equals 67.\n"
     ]
    }
   ],
   "source": [
    "print(agent.chat(\"What is 50 + 17?\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "async_add_numbers called\n",
      "44 + 16 equals 60.\n"
     ]
    }
   ],
   "source": [
    "print(await agent.achat(\"What is 44 + 16?\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
